package DengLu;

import Main.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class Entry extends  JFrame{

    private int length=0;
    private JLabel label1;
    private JLabel label2;
    private JPanel panel1;
    private JButton button1;
    private JButton button2;
    private JTextField textField1;
    private JTextField passwordField;


    public static void main(String args[]){
        Entry entry = new Entry();
        entry.setVisible(true);
    }


    public Entry(){
        super();
        setTitle("登录界面");
        setBounds(350,100,1200,800);
        getContentPane().setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1 = new JPanel();
        panel1.setBounds(0,0,1200,800);
        panel1.setBackground(Color.orange);
        panel1.setLayout(null);
        getContentPane().add(panel1);

        label1 = new JLabel("账号：");
        label1.setBounds(180 ,200,450,50);
        label1.setFont(new Font("",Font.BOLD,28));
        label1.setHorizontalAlignment(JLabel.CENTER);
        panel1.add(label1);

        label2 = new JLabel("密码：");
        label2.setBounds(180,300,450,50);
        label2.setFont(new Font("",Font.BOLD,28));
        label2.setHorizontalAlignment(JLabel.CENTER);
        panel1.add(label2);

        textField1 = new JTextField();
        textField1.setBounds(450,200,500,50);
        textField1.setFont(new Font("",Font.BOLD,22));
        textField1.setBackground(Color.orange);
        panel1.add(textField1);

        passwordField = new JTextField();
        passwordField.setBounds(450,300,500,50);
        passwordField.setFont(new Font("",Font.BOLD,22));
        passwordField.setBackground(Color.orange);
        panel1.add(passwordField);

        button1 = new JButton("登录");
        button1.setBounds(500,380,100,50);
        button1.setFont(new Font("",Font.BOLD,22));
        panel1.add(button1);
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object song = textField1.getText();//获得登录账号
                File user = new File("D://Account");//创建账号文件对象
                boolean flag1 = false,flag2 = false;
                try {
                    BufferedReader bufferedReader2 = new BufferedReader(new FileReader(user));
                    String string2;
                    string2 = bufferedReader2.readLine();
                    while (string2!=null){
                        string2 = bufferedReader2.readLine();
                        if (string2.equals(song)){//从账号文件中读取数据，如果相同，则置flag为true
                            flag1=true;
                            bufferedReader2.close();
                        }else {
                            break;
                        }
                    }

                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                Object passWordContain = passwordField.getText();//获取密码框中的内容
                File password = new File("D://Password");//创建密码框的文件对象
                try {
                    BufferedReader passwordReader = new BufferedReader(new FileReader(password));
                    String string ;
                    string = passwordReader.readLine();
                    while (string!=null){
                        string = passwordReader.readLine();
                        if (string.equals(passWordContain)){
                            flag2=true;
                            passwordReader.close();
                        }else{
                            break;
                        }
                    }
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                if (flag1&&flag2){//如果两个同时录入成功，则弹出主界面
                    dispose();
                    new Main().setVisible(true);
                }else if (song.equals("admin")&&passWordContain.equals("admin")){
                    dispose();
                    new Admin().setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(null,"请先注册或者重新输入！");

                }

            }
        });

        button2 = new JButton("注册");
        button2.setBounds(800,380,100,50);
        button2.setFont(new Font("",Font.BOLD,22));
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Register().setVisible(true);
            }
        });
        panel1.add(button2);

    }
}
